package com.mathassistant.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.ToAdminViewEvent;
import com.mathassistant.client.event.ToAdminViewEventHandler;
import com.mathassistant.client.event.ToUserViewEvent;
import com.mathassistant.client.event.ToUserViewEventHandler;
import com.mathassistant.client.event.topic.TopicChoosedEvent;
import com.mathassistant.client.event.topic.TopicChoosedEventHandler;
import com.mathassistant.client.helper.RPCCall;
import com.mathassistant.client.service.ExerciseServiceAsync;
import com.mathassistant.shared.ExerciseDTO;
import com.mathassistant.shared.TopicDTO;

public class ExercisePresenter implements Presenter {

	public interface Display
	{
		HasClickHandlers getAnswerRadioButton1();
		HasClickHandlers getAnswerRadioButton2();
		HasClickHandlers getAnswerRadioButton3();
		HasClickHandlers getAnswerRadioButton4();
		
		HasClickHandlers getCheckButton();
		HasClickHandlers getNextButton();
		
		void toCorrectView();
		void toWrongView();		
		void toEmptyView();
		void toFinishView();
		void radioSelected();
		
		void setData(ExerciseDTO exercise);
		void setExPerTotal(String indexPerTotal);
		
		Widget asWidget();
	}
	
	private int exNumber;
	private int exCount;
	private int choosedAnswer;
	private ExerciseDTO exercise;
	private TopicDTO topic;
	private Display display;
	private final ExerciseServiceAsync rpcService;
	private final SimpleEventBus eventBus;
	
	public ExercisePresenter(TopicDTO topic, ExerciseServiceAsync rpcService, 
			SimpleEventBus eventBus, Display view)
	{
		this.topic = topic;
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
		bind();
	}
	
	private void loadDefault(){
		fetchExerciseCount();
	}
	
	private void bind() {
		display.getAnswerRadioButton1().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choosedAnswer = 1;
				display.radioSelected();
			}
		});
		
		display.getAnswerRadioButton2().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choosedAnswer = 2;
				display.radioSelected();
			}
		});
		
		display.getAnswerRadioButton3().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choosedAnswer = 3;
				display.radioSelected();
			}
		});
		
		display.getAnswerRadioButton4().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				choosedAnswer = 4;
				display.radioSelected();
			}
		});
		
		display.getCheckButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(choosedAnswer == exercise.getAnswer())
				{
					display.toCorrectView();
				}
				else{
					display.toWrongView();
				}
			}
		});
		
		display.getNextButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				exNumber++;
				if(exNumber >= exCount){
					display.toFinishView();
					display.setExPerTotal("Hoàn thành :" + exCount + "/" + exCount+" .");
				}
				else
				{
					nextExercise();
					display.setExPerTotal("Bạn đang xem bài " + (exNumber+1) + 
							" trên tổng " + exCount + " bài tập.");
				}
			}
		});
		
		eventBus.addHandler(ToUserViewEvent.TYPE, new ToUserViewEventHandler() {
			
			@Override
			public void onTopicListToUserView(ToUserViewEvent event) {
				// TODO Auto-generated method stub
				if(topic != null)
					eventBus.fireEvent(new TopicChoosedEvent(topic));
			}
		});
		
		eventBus.addHandler(ToAdminViewEvent.TYPE, new ToAdminViewEventHandler() {
			
			@Override
			public void onTopicListToAdminView(ToAdminViewEvent event) {
				// TODO Auto-generated method stub
				if(topic != null)
					eventBus.fireEvent(new TopicChoosedEvent(topic));
			}
		});
		
		eventBus.addHandler(TopicChoosedEvent.TYPE, new TopicChoosedEventHandler() {
			
			@Override
			public void onTopicChoosed(TopicChoosedEvent event) {
				// TODO Auto-generated method stub
				exCount = 0;
				exNumber = 0;
			}
		});
	}
	
	public void nextExercise(){
		fetchExerciseDTO();
	}
	
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		loadDefault();
	}

	@Override
	public void go() {

	}
	void fetchExerciseDTO()
	{
		new RPCCall<ExerciseDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error fetch exercise: " + caught.getMessage());
			}

			@Override
			public void onSuccess(ExerciseDTO result) {
				if(result != null){
					exercise = result;
					display.setData(exercise);
				}
			}

			@Override
			protected void callService(AsyncCallback<ExerciseDTO> cb) {
				rpcService.getNextExercise(exNumber, topic, cb);
			}
		}.retry(3);
	}
	
	void fetchExerciseCount()
	{
		new RPCCall<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Error fetch exercise: " + caught.getMessage());
			}

			@Override
			public void onSuccess(Integer result) {
				// TODO Auto-generated method stub
				exCount = result;
				if(exCount > 0){
					display.setExPerTotal("Bạn đang xem bài " + (exNumber+1) + 
						" trên tổng " + exCount + " bài tập.");
					fetchExerciseDTO();
				}
				else{
					display.toEmptyView();
				}
			}

			@Override
			protected void callService(AsyncCallback<Integer> cb) {
				// TODO Auto-generated method stub
				rpcService.getCount(topic, cb);
			}
		}.retry(3);
	}
}
